package com.dhcc.DRGsHosp.application.impl;

import com.dhcc.DRGsHosp.application.MrExpenseApplication;
import com.dhcc.DRGsHosp.core.domain.MrExpense;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 费用信息 的  数据访问层--实体类
 * @ClassName: MrExpenseApplicationImpl  
 * @Description ：费用信息  数据访问层 实体类    
 * @Date: 2018年7月26日 下午9:39:08  
 * @Author QiaoYiLong 
 * @Version 3.1.1 DHC-DRG
 */

@Named("drgsHospMrExpenseApplicationImpl20181212")
@Transactional
public class MrExpenseApplicationImpl implements MrExpenseApplication {
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
	/** 
	 *  功能描述：取得主页索引为pmainIndex的所有 费用信息
	 *  @Author QiaoYiLong
	 *  @param pmainIndex
	 *  @Date:2018年7月26日下午9:34:59
	 */
	@Override
	public List<MrExpense> getMrExpensesByPmainIndex(String pmainIndex) {
		if( pmainIndex == null || "".equals(pmainIndex) ){
			return null;
		}

		try{
			String jpql = " select _expense from MrExpense _expense where _expense.pmainIndex = '"+pmainIndex+"' ";
			List<MrExpense> all = getQueryChannelService().createJpqlQuery(jpql).list();
			return all;
		}catch(Exception ex){
			ex.printStackTrace();
			return new ArrayList<>();
		}
	}

	/**
	 * @param condition
	 * @return
	 * @throws
	 * @Description: 查询费用信息
	 * @author chenhanlin
	 * @date 2018/7/29 15:51
	 */
	@Override
	public List<MrExpense> queryMrExpenseByCondition(Map<String, Object> condition) {
		// 分页sql
		String pageSqlBody = " ( SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM ( SELECT a.PMAININDEX FROM  drgs.MR_BASEINFO a LEFT JOIN drgs.MR_IPINFO ipinfo ON a.PMAININDEX = ipinfo.PMAININDEX WHERE   1=1  ORDER BY ipinfo.PMAININDEX desc) PAGERESULT ) WHERE rn>(:pageNo-1)*:pageSize and rn <=:pageNo*:pageSize )page" ;

		StringBuffer sqlb = new StringBuffer() ;
		sqlb.append(" select  ID \"id\", e.PMAININDEX \"pmainIndex\", PETYPE \"peType\", PEINDEX \"peIndex\", PE0100 \"pe0100\", PE0101 \"pe0101\", PE0200 \"pe0200\"\n") ;
		sqlb.append(" FROM \n") ;
		sqlb.append(" drgs.MR_EXPENSE e\n") ;
		sqlb.append(" right join "+pageSqlBody+" ON page.PMAININDEX = e.PMAININDEX \n" ) ;
		String sql = sqlb.toString() ;
		// 时间条件
		if(condition.get("dateFrom")!=null && condition.get("dateTo") !=null){
			String dateField = "" ;
			// 时间字段
			if("outDate".equals(condition.get("dateType"))){ // 出院日期
				dateField = "ipinfo.P00290000" ;
			} else if("pushDate".equals(condition.get("dateType"))) { // 推送日期
				dateField = "a.push_date" ;
			}
			sql = sql.replace("1=1", " 1=1 and to_char("+dateField+",'yyyy-mm-dd')  BETWEEN :dateFrom AND :dateTo ") ;
		}

		if(condition.get("hospCode") != null){
			sql = sql.replace("1=1" ," 1=1 and instr(:hospCode,a.p00010001)>0 ") ;
		}

		// 分页sql
		String pageSqlHead = "SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM (";
		String pageSqlFoot = " ORDER BY ipinfo.PMAININDEX desc) PAGERESULT ) WHERE rn>(:pageNo-1)*:pageSize and rn <=:pageNo*:pageSize ";
		List<MrExpense> mrExpenseList = jdbcTempSeverice.query(sql,condition , new BeanPropertyRowMapper<MrExpense>(MrExpense.class));
		return mrExpenseList;
	}

}
